home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / js / calWcapRequest.js < prev    next >
Text File  |  2008-01-16  |  15KB  |  444 lines

  1. /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Sun Microsystems code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Sun Microsystems, Inc.
  19.  * Portions created by the Initial Developer are Copyright (C) 2007
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  24.  *   Philipp Kewisch <mozilla@kewis.ch>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /**
  41.    A request object is used to track an async action.
  42.    While the action is running, isPending is true.
  43.    Functions issuing an async action usually take a response function along
  44.    with their parameters, typically named respFunc.
  45.    That function is called *after* the action has ended (i.e. isPending of the
  46.    issued action/request is false when called, status remains stable).
  47.    The response function gets the ended request as first parameter to check
  48.    whether the request has been successful and get its data.
  49.    The request function itself may return either
  50.    - a further calIOperation request object, i.e. an async continuation
  51.    - some data (incl null/undefined) which is the result of the async function,
  52.      indicating that there is no further continuation
  53. */
  54.  
  55. var g_requestPrefix = null;
  56. var g_requestId = 0;
  57. function generateRequestId() {
  58.     if (!g_requestPrefix) {
  59.         g_requestPrefix = (getUUID() + "-");
  60.     }
  61.     ++g_requestId;
  62.     return (g_requestPrefix + g_requestId);
  63. }
  64.  
  65. function calWcapRequest(respFunc, logContext) {
  66.     this.wrappedJSObject = this;
  67.     this.m_logContext = logContext;
  68.     this.m_id = generateRequestId();
  69.     this.m_isPending = true;
  70.     this.m_status = NS_OK;
  71.     this.m_respFunc = respFunc;
  72.     this.m_attachedRequests = [];
  73. }
  74. calWcapRequest.prototype = {
  75.     m_logContext: null,
  76.     m_parentRequest: null,
  77.     m_id: 0,
  78.     m_isPending: true,
  79.     m_status: NS_OK,
  80.     m_respFunc: null,
  81.     m_attachedRequests: null,
  82.     m_locked: false,
  83.     
  84.     get parentRequest() { return this.m_parentRequest; },
  85.     set parentRequest(req) {
  86.         if (this.parentRequest)
  87.             logError("already has parent!", this);
  88.         this.detachFromParent(); // detach without error
  89.         return (this.m_parentRequest = req);
  90.     },
  91.     
  92.     /** The following locking is necessary when scheduling multiple async
  93.         requests; one cannot be sure that e.g. the first completes quickly
  94.         and responds the whole parent request when detaching.
  95.     */
  96.     lockPending: function calWcapRequest_lockPending() {
  97.         this.m_locked = true;
  98.     },
  99.     unlockPending: function calWcapRequest_unlockPending() {
  100.         if (this.m_locked) {
  101.             this.m_locked = false;
  102.             // assures that respFunc is executed:
  103.             if (this.m_attachedRequests.length == 0) {
  104.                 this.execRespFunc();
  105.             }
  106.         }
  107.     },
  108.     
  109.     toString: function calWcapRequest_toString() {
  110.         var ret = ("calWcapRequest id=" + this.id +
  111.                    ", parent-id=" +
  112.                    (this.parentRequest ? this.parentRequest.id : "<none>") +
  113.                    " (" + this.m_logContext + ")");
  114.         if (LOG_LEVEL > 2 && this.m_attachedRequests.length > 0) {
  115.             ret += "\nattached requests:";
  116.             for each ( var req in this.m_attachedRequests ) {
  117.                 ret += ("\n#" + req.id + "\t" + req);
  118.             }
  119.         }
  120.         ret += (", isPending=" + this.isPending);
  121.         ret += (", status=" + errorToString(this.status));
  122.         return ret;
  123.     },
  124.     
  125.     attachSubRequest: function calWcapRequest_attachSubRequest(req)
  126.     {
  127.         if (req) {
  128.             if (!this.m_attachedRequests.some(
  129.                     function(req_) { return req.id == req_.id; } )) {
  130.                 if (req.isPending) {
  131.                     this.m_attachedRequests.push(req);
  132.                     req.parentRequest = this;
  133.                     log("attachSubRequest()", this);
  134.                 }
  135.                 else if (!this.m_locked && this.m_attachedRequests.length == 0) {
  136.                     this.execRespFunc(req.status);
  137.                 }
  138.             }
  139.             else {
  140.                 logError("request already attached: " + req.id, this);
  141.             }
  142.         }
  143.     },
  144.     
  145.     detachSubRequest: function calWcapRequest_detachSubRequest(req, err)
  146.     {
  147.         this.m_attachedRequests = this.m_attachedRequests.filter(
  148.             function(req_) { return req.id != req_.id; } );
  149.         if (err) {
  150.             // first failing sub request stops parent request:
  151.             this.execRespFunc(err);
  152.         }
  153.         // assures that respFunc is executed after every sub request has been completed:
  154.         else if (!this.m_locked && this.m_attachedRequests.length == 0) {
  155.             this.execRespFunc();
  156.         }
  157.     },
  158.     
  159.     cancelAllSubRequests: function calWcapRequest_cancelAllSubRequests(status) {
  160.         var attachedRequests = this.m_attachedRequests;
  161.         this.m_attachedRequests = [];
  162.         attachedRequests.forEach( function(req) { req.cancel(null); } );
  163.     },
  164.     
  165.     detachFromParent: function calWcapRequest_detachFromParent(err) {
  166.         var parentRequest = this.m_parentRequest;
  167.         if (parentRequest) {
  168.             this.m_parentRequest = null;
  169.             parentRequest.detachSubRequest(this, err);
  170.         }
  171.     },
  172.     
  173.     execRespFunc: function calWcapRequest_execRespFunc(err, data)
  174.     {
  175.         if (this.isPending) {
  176.             this.m_isPending = false;
  177.             if (err)
  178.                 this.m_status = err;
  179.             this.cancelAllSubRequests();
  180.             var respFunc = this.m_respFunc;
  181.             if (respFunc) {
  182.                 this.m_respFunc = null; // call once only
  183.                 if (LOG_LEVEL > 2) {
  184.                     log("response exec: " + errorToString(err), this);
  185.                 }
  186.                 try {
  187.                     respFunc(this, err, data);
  188.                 }
  189.                 catch (exc) {
  190.                     this.m_status = exc;
  191.                     // don't pump into error console, may be handled:
  192.                     log("error: " + errorToString(exc), this);
  193.                 }
  194.             }
  195.             this.detachFromParent(this.m_status);
  196.         }
  197.     },
  198.  
  199.     execSubRespFunc: function calWcapRequest_execSubRespFunc(func, err, data) {
  200.         try {
  201.             func(err, data);
  202.         } catch (exc) {
  203.             this.execRespFunc(exc);
  204.         }
  205.     },
  206.  
  207.     // calIOperation:
  208.     get id() {
  209.         return this.m_id;
  210.     },
  211.     get isPending() {
  212.         return this.m_isPending;
  213.     },
  214.     get status() {
  215.         return (this.m_status === null ? NS_OK : this.m_status);
  216.     },
  217.     
  218.     cancel: function calWcapRequest_cancel(status) {
  219.         if (!status)
  220.             status = calIErrors.OPERATION_CANCELLED;
  221.         this.execRespFunc(status);
  222.     }
  223. };
  224.  
  225. function calWcapNetworkRequest(channel, respFunc, bLogging) {
  226.     this.wrappedJSObject = this;
  227.     this.m_id = generateRequestId();
  228.     this.m_channel = channel;
  229.     this.m_respFunc = respFunc;
  230.     this.m_bLogging = (bLogging === undefined ? true : bLogging);
  231. }
  232.  
  233. calWcapNetworkRequest.prototype = {
  234.     m_id: 0,
  235.     m_channel: null,
  236.     m_respFunc: null,
  237.     m_bLogging: false,
  238.     
  239.     toString: function calWcapNetworkRequest_toString() {
  240.         var ret = ("calWcapNetworkRequest id=" + this.id +
  241.                    ", parent-id=" +
  242.                    (this.parentRequest ? this.parentRequest.id : "<none>"));
  243.         if (this.m_bLogging)
  244.             ret += (" (" + this.m_channel.URI.spec + ")");
  245.         ret += (", isPending=" + this.isPending);
  246.         ret += (", status=" + errorToString(this.status));
  247.         return ret;
  248.     },
  249.     
  250.     m_parentRequest: null,
  251.     get parentRequest() { return this.m_parentRequest; },
  252.     set parentRequest(req) {
  253.         if (this.parentRequest)
  254.             logError("already has parent!", this);
  255.         this.detachFromParent(); // detach without error
  256.         return (this.m_parentRequest = req);
  257.     },
  258.  
  259.     // calIOperation:
  260.     get id() {
  261.         return this.m_id;
  262.     },
  263.     
  264.     m_isPending: true,
  265.     get isPending() { return this.m_isPending; },
  266.     
  267.     get status() {
  268.         return this.m_channel.status;
  269.     },
  270.     
  271.     detachFromParent: function calWcapNetworkRequest_detachFromParent(err) {
  272.         var parentRequest = this.m_parentRequest;
  273.         if (parentRequest) {
  274.             this.m_parentRequest = null;
  275.             parentRequest.detachSubRequest(this, err);
  276.         }
  277.     },
  278.     
  279.     cancel: function calWcapNetworkRequest_cancel(status) {
  280.         if (!status)
  281.             status = calIErrors.OPERATION_CANCELLED;
  282.         this.execRespFunc(status);
  283.         // xxx todo: check whether this works on redirected channels!
  284.         if (this.m_channel.isPending()) {
  285.             log("canceling netwerk request...", this);
  286.             this.m_channel.cancel(NS_BINDING_FAILED);
  287.         }
  288.     },
  289.     
  290.     execRespFunc: function calWcapNetworkRequest_execRespFunc(err, str)
  291.     {
  292.         if (this.isPending) {
  293.             this.m_isPending = false;
  294.             var respFunc = this.m_respFunc;
  295.             if (respFunc) {
  296.                 this.m_respFunc = null; // call once only
  297.                 if (LOG_LEVEL > 2 && this.m_bLogging) {
  298.                     log("response exec: " + errorToString(err), this);
  299.                 }
  300.                 try {
  301.                     respFunc(err, str);
  302.                     err = null; // may have been handled
  303.                 }
  304.                 catch (exc) {
  305.                     // don't pump into error console, may be handled:
  306.                     log("error: " + errorToString(exc), this);
  307.                     err = exc;
  308.                 }
  309.             }
  310.             this.detachFromParent(err);
  311.         }
  312.     },
  313.  
  314.     execSubRespFunc: function calWcapNetworkRequest_execSubRespFunc(func, err, data) {
  315.         try {
  316.             func(err, data);
  317.         } catch (exc) {
  318.             this.execRespFunc(exc);
  319.         }
  320.     },
  321.  
  322.     // nsIUnicharStreamLoaderObserver:
  323.     onDetermineCharset: function calWcapNetworkRequest_onDetermineCharset(
  324.         loader, context, firstSegment, length)
  325.     {
  326.         var channel = null;
  327.         if (loader)
  328.             channel = loader.channel;
  329.         var charset = null;
  330.         if (channel)
  331.             charset = channel.contentCharset;
  332.         if (!charset || charset.length == 0)
  333.             charset = "UTF-8";
  334.         return charset;
  335.     },
  336.     
  337.     onStreamComplete: function calWcapNetworkRequest_onStreamComplete(
  338.         loader, context, status, /* nsIUnicharInputStream */ unicharData)
  339.     {
  340.         if (LOG_LEVEL > 0 && this.m_bLogging) {
  341.             log("status: " + errorToString(status), this);
  342.         }
  343.         switch (status) {
  344.         case NS_BINDING_SUCCEEDED: {
  345.             var err = null;
  346.             var str = "";
  347.             try {
  348.                 if (unicharData) {
  349.                     var str_ = {};
  350.                     while (unicharData.readString(-1, str_)) {
  351.                         str += str_.value;
  352.                     }
  353.                 }
  354.                 if (LOG_LEVEL > 2 && this.m_bLogging) {
  355.                     log("contentCharset = " + this.onDetermineCharset(loader) +
  356.                         "\nrequest result:\n" + str, this);
  357.                 }
  358.             }
  359.             catch (exc) {
  360.                 err = exc;
  361.             }
  362.             this.execRespFunc(err, str);
  363.             break;
  364.         }
  365.         case NS_BINDING_REDIRECTED:
  366.         case NS_BINDING_RETARGETED:
  367.             // just status
  368.             // xxx todo: in case of a redirected channel,
  369.             // how to get that channel => cancel feature!
  370.             break;
  371.         default: // errors:
  372.             this.execRespFunc(status);
  373.             break;
  374.         }
  375.     }
  376. };
  377.  
  378. function issueNetworkRequest(parentRequest, respFunc, url, bLogging)
  379. {
  380.     var channel;
  381.     try {
  382.         var loader = Components.classes["@mozilla.org/network/unichar-stream-loader;1"]
  383.                                .createInstance(Components.interfaces.nsIUnicharStreamLoader);
  384.         channel = getIOService().newChannel(url, "" /* charset */, null /* baseURI */);
  385.         channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
  386.     }
  387.     catch (exc) {
  388.         respFunc(exc);
  389.         return;
  390.     }
  391.     var netRequest = new calWcapNetworkRequest(channel, respFunc, bLogging);
  392.     parentRequest.attachSubRequest(netRequest);
  393.     log("opening channel.", netRequest);
  394.     try {
  395.         loader.init(channel, netRequest, null /*context*/, 0 /*segment size*/);
  396.     }
  397.     catch (exc) {
  398.         netRequest.execRespFunc(exc);
  399.     }
  400. }
  401.  
  402. function getWcapRequestStatusString(xml)
  403. {
  404.     var str = "request status: ";
  405.     var items = xml.getElementsByTagName("RSTATUS");
  406.     if (items != null && items.length > 0)
  407.         str += items.item(0).textContent;
  408.     else
  409.         str += "none";
  410.     return str;
  411. }
  412.  
  413. function stringToIcal(session, data, expectedErrno)
  414. {
  415.     if (!data || data.length == 0) { // assuming time-out; WTF.
  416.         throw new Components.Exception(
  417.             "Login failed. Invalid session ID.",
  418.             Components.interfaces.calIWcapErrors.WCAP_LOGIN_FAILED);
  419.     }
  420.     var icalRootComp;
  421.     try {
  422.         icalRootComp = getIcsService().parseICS(data, session /*implements calITimezoneProvider*/);
  423.     }
  424.     catch (exc) { // map into more useful error string:
  425.         throw new Components.Exception("error parsing ical data!",
  426.                                        Components.interfaces.calIErrors.ICS_PARSE);
  427.     }
  428.     checkWcapIcalErrno(icalRootComp, expectedErrno);
  429.     return icalRootComp;
  430. }
  431.  
  432. function stringToXml(session, data, expectedErrno)
  433. {
  434.     if (!data || data.length == 0) { // assuming time-out
  435.         throw new Components.Exception(
  436.             "Login failed. Invalid session ID.",
  437.             Components.interfaces.calIWcapErrors.WCAP_LOGIN_FAILED);
  438.     }
  439.     var xml = getDomParser().parseFromString(data, "text/xml");
  440.     checkWcapXmlErrno(xml, expectedErrno);
  441.     return xml;
  442. }
  443.  
  444.